<?php

Class _Seed extends Connect{
    
    var $table = "seed";
    
    /**
     * user, habar 
    */
    function add($user, $id, $count = 1){        
        $sql = "INSERT INTO `".PREFIX_TABLE.$this->table."` 
            (`user`, `parent`, `use` ) VALUES ";
        for($i = 1; $i <= $count; $i++ ){
            $sql .= "('".$user."', '".$id."', '0')";
            $sql .= ($i < $count)? " , " : "" ;
        }
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        return true; 
    }
    
    function _isit($user, $id){
        $sql = "SELECT * FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `user` = '".$user."' AND `parent` = '".$id."'";
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        $arr = DBConnect::getOne($result);
        if(!isset($arr['id'])) return false;
        return true;
    }
    
    function del($user, $id, $count = 1){
        $sql = "DELETE FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `parent` = '".$id."' AND `user` = '".$user."' LIMIT ".intval($count);
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        return true;        
    }
    
    function getMy($user, $id = false){
        $sql = "SELECT * FROM `".PREFIX_TABLE.$this->table."` 
        WHERE `user` = '".$user."' ";
        if($id){
            $sql .= " AND `parent` = '".$id."' ";
        }
        #_echo($sql);
        #return $sql;
        if(!$result = DBConnect::query($sql)){
            #return $glade;
            $this->_error(mysql_error());
            return false;
        }
        #return
        $_arr = array();
        while($arr = DBConnect::getOne($result)){
            #_echo("id > ".$arr['id']);
            $_arr[] = $arr;    
        } 

        return $_arr;
    }    
    
    function _use($user, $id){
        $sql = "UPDATE `".PREFIX_TABLE.$this->table."` SET
            `use` = '1' WHERE `use` = '0' AND `user` = '".$user."'
            AND `parent` = '".$id."'
            LIMIT 1  ";
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;
    }
    
    
}
?>